import cv2
import numpy as np
from matplotlib import pyplot as plt
import time

from PIL import ImageGrab

import cv2
import numpy as np
# from matplotlib import pyplot as plt
import autopy3 as autopy
import time, threading
from threading import Thread
凶暴野兽闪光 = cv2.imread('狂暴野兽闪光.png', 0)
狂暴野兽 = cv2.imread('狂暴野兽.png', 0)

野兽状态=0
战斗状态=0

def 截图():
    time1 = time.time()
    img_rgb = np.array(ImageGrab.grab().convert('RGB'))# 截图
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)#灰度
    time2 = time.time()
    # print(time2 - time1)
    return (img_gray,img_rgb)

def 比较(img_gray,template,img_rgb ,阈值):
    res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)  # 大图 ，目标 ，模式
    threshold = 阈值 # 阈值
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        # w, h = template.shape[::-1]
        # cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), )
        # cv2.imwrite('res.png', img_rgb)
        #
        # autopy.key.toggle(0x15, True, )
        # autopy.key.toggle(0x15, False, )
        # print(threading.current_thread().name ,True)
        return (True)
    # print(threading.current_thread().name,False)
    return (False)
# im0 =ImageGrab.grab((0, 0, 2880, 1800))
# im0.show()
# im0 =ImageGrab.grab((0, 1500, 2880, 1800))
# im0.show()


class MyThread(Thread):
    def __init__(self, img_gray, 狂暴野兽, img_rgb, 阈值):
        Thread.__init__(self)
        # self.number = number

    def run(self):
        self.result = 比较(img_gray, 狂暴野兽, img_rgb, 阈值)

    def get_result(self):
        return self.result




img_gray, img_rgb =截图()
比较(img_gray, 狂暴野兽, img_rgb, 0.7)